<template>
  <div class="login-container">
    <el-card class="login-card" shadow="hover">
      <div class="logo-section">
        <!-- 建议换成你自己的鞋城logo -->
        <img src="/src/photos/logo.jpg" alt="鞋城Logo" class="logo" />
        <h2 class="title">鞋城履行 · 登录</h2>
      </div>

      <el-form
        ref="loginFormRef"
        :model="user"
        :rules="rules"
        label-width="80px"
        class="login-form"
      >
        <el-form-item label="账号" prop="username">
          <el-input
            v-model="user.username"
            placeholder="请输入账号"
            :prefix-icon="User"
          />
        </el-form-item>

        <el-form-item label="密码" prop="pwd">
          <el-input
            v-model="user.password"
            :type="pwdVisible ? 'text' : 'password'"
            placeholder="请输入密码"
            :prefix-icon="Lock"
          >
            <template #suffix>
              <el-icon @click="pwdVisible = !pwdVisible" class="eye-icon">
                <component :is="pwdVisible ? View : Hide" />
              </el-icon>
            </template>
          </el-input>
        </el-form-item>

        <!-- 记住账号 & 忘记密码 -->
        <div class="extra-options">
          <el-checkbox v-model="rememberMe">记住账号</el-checkbox>
          <span class="forgot-link" @click="goForgotPwd">忘记密码？</span>
        </div>

        <el-form-item class="buttons">
          <el-button type="primary" @click="handleLogin(loginFormRef)" class="login-btn">登录</el-button>
          <el-button @click="$router.push('/register')" class="register-btn">注册</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script setup>
import { ref, onMounted, reactive } from 'vue'
import { useRouter } from 'vue-router'
import { User, Lock, View, Hide } from '@element-plus/icons-vue'
import { login } from '@/api/User'
const router = useRouter()
const loginFormRef = ref()

const user = ref({
  username: '',
  password: '',
  uid:''

})
const rememberMe = ref(false)
const pwdVisible = ref(false)

// 校验规则
const rules = {
  username: [
    { required: true, message: '请输入账号', trigger: 'blur' },
    { pattern: /^[a-zA-Z_$]\w{2,11}$/, message: '账号必须以字母/下划线开头，3-12位', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 6, max: 16, message: '密码长度需为6-16位', trigger: 'blur' }
  ]
}

// 页面加载时回填账号
onMounted(() => {
  const savedUser = localStorage.getItem('shoeCityUser')
  if (savedUser) {
    user.value.username = savedUser
    user.value.uid=savedUser
    rememberMe.value = true
  }
})


function handleLogin(formEl) {
  if (!formEl) return
  formEl.validate((valid, fields) => {
    if (valid) {
      console.log(user.value.username, user.value.password)
      login(user.value.username, user.value.password).then(res => {
        console.log(res)
        // 登录成功逻辑
        localStorage.setItem('shoeCityUid', res.uid)
        console.log(res.uid)
  // 1. 存储uid到localStorage（关键）
         // 👉 记住账号逻辑
        if (rememberMe.value) {
          localStorage.setItem('shoeCityUser', user.value.username)
         
        } else {
          localStorage.removeItem('shoeCityUser')
        }

        // 跳转
        router.push({ path: "/main" })
      })
    } else {
      console.log('error submit!', fields)
    }
  })
}


function goForgotPwd() {
  router.push('/forgot-password')
}
</script>

<style scoped>
/* 整体背景：鞋店氛围图 */
.login-container {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
  background: linear-gradient(rgba(0,0,0,0.4), rgba(0,0,0,0.5)),
    url('https://images.unsplash.com/photo-1587463260925-87d79811e8da?auto=format&fit=crop&w=1600&q=80');
  background-size: cover;
  background-position: center;
}

/* 卡片 */
.login-card {
  width: 420px;
  padding: 35px 30px;
  border-radius: 18px;
  background: rgba(255, 255, 255, 0.95);
  backdrop-filter: blur(8px);
  box-shadow: 0 8px 30px rgba(0,0,0,0.2);
  transition: all 0.3s ease;
}
.login-card:hover {
  transform: translateY(-6px);
}

/* Logo部分 */
.logo-section {
  text-align: center;
  margin-bottom: 20px;
}
.logo {
  width: 85px;
  height: 85px;
  margin-bottom: 12px;
}
.title {
  font-family: 'Segoe UI', 'Helvetica Neue', Arial, sans-serif;
  font-size: 26px;
  font-weight: bold;
  color: #4a3f35;
  letter-spacing: 1px;
}

/* 表单 */
.login-form {
  margin-top: 15px;
}
.el-input {
  border-radius: 25px;
  background-color: #fffaf5;
  padding: 2px 10px;
  transition: 0.3s;
}
.el-input:hover {
  box-shadow: 0 0 6px rgba(139, 111, 75, 0.3);
}
.eye-icon {
  cursor: pointer;
  color: #8b6f4b;
  transition: 0.3s;
}
.eye-icon:hover {
  color: #4a3f35;
}

/* 记住账号 + 忘记密码 */
.extra-options {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin: -5px 0 15px 0;
  font-size: 13px;
  color: #666;
}
.forgot-link {
  cursor: pointer;
  color: #8b6f4b;
  font-weight: bold;
  transition: 0.3s;
}
.forgot-link:hover {
  color: #4a3f35;
  text-decoration: underline;
}

/* 按钮 */
.buttons {
  display: flex;
  justify-content: space-between;
  margin-top: 10px;
}
.login-btn {
  width: 65%;
  border-radius: 25px;
  background: linear-gradient(135deg, #8b6f4b, #b9976b);
  border: none;
  font-weight: bold;
  font-size: 16px;
  transition: 0.3s;
}
.login-btn:hover {
  background: linear-gradient(135deg, #725838, #9e7b4e);
}
.register-btn {
  width: 30%;
  border-radius: 25px;
  border: 2px solid #8b6f4b;
  color: #8b6f4b;
  font-weight: bold;
  font-size: 14px;
  background: transparent;
  transition: 0.3s;
}
.register-btn:hover {
  background: #8b6f4b;
  color: white;
}
</style>